#include "hplcinjectprogrampage.h"
#include <QVBoxLayout>
#include <QHBoxLayout>

HplcInjectProgramPage::HplcInjectProgramPage(QWidget *parent) : QWidget(parent)
{
    setWindowTitle(tr("Hplc Inject Program Page"));
    setAttribute(Qt::WA_DeleteOnClose);

    m_gb = new QGroupBox(tr("Inject Pump(Pump D)"));
    m_lbFlow = new QLabel(tr("Inejct Flow(ml/min)"));
    m_leFlow = new QLineEdit();
    connect(m_leFlow, &QLineEdit::editingFinished, this, [=](){
        double flow = m_leFlow->text().toDouble();
        if (m_flow != flow)
        {
            m_flow = flow;
            emit flowChanged(m_flow);
        }
    });
    m_lbTime = new QLabel(tr("Inject Time(min)"));
    m_leTime = new QLineEdit();
    connect(m_leTime, &QLineEdit::editingFinished, this, [=](){
        double time = m_leTime->text().toDouble();
        if (m_time != time)
        {
            m_time = time;
            emit timeChanged(m_time);
        }
    });

    QHBoxLayout *hblFlow = new QHBoxLayout();
    hblFlow->addWidget(m_lbFlow);
    hblFlow->addWidget(m_leFlow);
    QHBoxLayout *hblTime = new QHBoxLayout();
    hblTime->addWidget(m_lbTime);
    hblTime->addWidget(m_leTime);
    QVBoxLayout *vblGroup = new QVBoxLayout();
    vblGroup->addLayout(hblFlow);
    vblGroup->addLayout(hblTime);
    m_gb->setLayout(vblGroup);

    m_pbStart = new QPushButton(tr("Start"));
    connect(m_pbStart, &QPushButton::clicked, this, [=](){
        close();
        emit requestStart();
    });
    m_pbReturn = new QPushButton(tr("Return"));
    connect(m_pbReturn, &QPushButton::clicked, this, [=](){
        close();
    });
    QHBoxLayout *hblStart = new QHBoxLayout();
    hblStart->addWidget(m_pbStart);
    hblStart->addWidget(m_pbReturn);

    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->addWidget(m_gb);
    vbl->addLayout(hblStart);
    setLayout(vbl);
}

void HplcInjectProgramPage::setParam(double flow, double time)
{
    m_flow = flow;
    m_leFlow->setText(QString::number(flow));
    m_time = time;
    m_leTime->setText(QString::number(time));
}

void HplcInjectProgramPage::setRunning(bool isRunning)
{
    m_leFlow->setEnabled(!isRunning);
    m_leTime->setEnabled(!isRunning);
    m_pbStart->setEnabled(!isRunning);
}
